package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeIntakeAirTemp;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeIntakeAirTempEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 发动机进气温度区间算法<br>
 * 输出：统计<br>
 * @author 沈东生
 */
public class RangeIntakeAirTemp extends AbstractArithmetic<VarRangeIntakeAirTemp> {
    @Override
    public void calculate() {
        logger.debug("进入发动机进气温度区间算法.");
        CommonData data = drivingData.getCommonData();
        Integer intakeAirTem = data.getIntakeAirTem();
        if (intakeAirTem == null) {
            return;
        }

        if (!RangeIntakeAirTempEnum.RANGE_VALID.getRange().contains(intakeAirTem)) {
            return;
        }

        int duration = data.getDffTime();
        VarRangeIntakeAirTemp var = getObj();
        if (RangeIntakeAirTempEnum.RANGE_50.getRange().contains(intakeAirTem)) {
            var.setIat50Duration(
                    Optional.ofNullable(var.getIat50Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_70.getRange().contains(intakeAirTem)) {
            var.setIat70Duration(
                    Optional.ofNullable(var.getIat70Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_80.getRange().contains(intakeAirTem)) {
            var.setIat80Duration(
                    Optional.ofNullable(var.getIat80Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_85.getRange().contains(intakeAirTem)) {
            var.setIat85Duration(
                    Optional.ofNullable(var.getIat85Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_90.getRange().contains(intakeAirTem)) {
            var.setIat90Duration(
                    Optional.ofNullable(var.getIat90Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_95.getRange().contains(intakeAirTem)) {
            var.setIat95Duration(
                    Optional.ofNullable(var.getIat95Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_100.getRange().contains(intakeAirTem)) {
            var.setIat100Duration(
                    Optional.ofNullable(var.getIat100Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_105.getRange().contains(intakeAirTem)) {
            var.setIat105Duration(
                    Optional.ofNullable(var.getIat105Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_110.getRange().contains(intakeAirTem)) {
            var.setIat110Duration(
                    Optional.ofNullable(var.getIat110Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_115.getRange().contains(intakeAirTem)) {
            var.setIat115Duration(
                    Optional.ofNullable(var.getIat115Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_120.getRange().contains(intakeAirTem)) {
            var.setIat120Duration(
                    Optional.ofNullable(var.getIat120Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_125.getRange().contains(intakeAirTem)) {
            var.setIat125Duration(
                    Optional.ofNullable(var.getIat125Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_130.getRange().contains(intakeAirTem)) {
            var.setIat130Duration(
                    Optional.ofNullable(var.getIat130Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_135.getRange().contains(intakeAirTem)) {
            var.setIat135Duration(
                    Optional.ofNullable(var.getIat135Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_140.getRange().contains(intakeAirTem)) {
            var.setIat140Duration(
                    Optional.ofNullable(var.getIat140Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_145.getRange().contains(intakeAirTem)) {
            var.setIat145Duration(
                    Optional.ofNullable(var.getIat145Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_150.getRange().contains(intakeAirTem)) {
            var.setIat150Duration(
                    Optional.ofNullable(var.getIat150Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirTempEnum.RANGE_OVER_150.getRange().contains(intakeAirTem)) {
            var.setIatOver150Duration(
                    Optional.ofNullable(var.getIatOver150Duration()).orElse(0) + duration);
        }

        if (var.getMaxIat() == null  || intakeAirTem > var.getMaxIat()) {
            var.setMaxIat(intakeAirTem);
        }
        if (var.getMinIat() == null || intakeAirTem < var.getMinIat()) {
            var.setMinIat(intakeAirTem);
        }
        var.setIatSum(Optional.ofNullable(var.getIatSum()).orElse(0L) + intakeAirTem);
        var.setIatCount(Optional.ofNullable(var.getIatCount()).orElse(0) + 1);
        logger.debug("离开发动机进气温度区间算法.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeIntakeAirTemp var = getObj();
        //iat
        statisticDataOut.setIat50Duration(ArithmeticUtils.timeMsToSecond(var.getIat50Duration()));
        statisticDataOut.setIat70Duration(ArithmeticUtils.timeMsToSecond(var.getIat70Duration()));
        statisticDataOut.setIat80Duration(ArithmeticUtils.timeMsToSecond(var.getIat80Duration()));
        statisticDataOut.setIat85Duration(ArithmeticUtils.timeMsToSecond(var.getIat85Duration()));
        statisticDataOut.setIat90Duration(ArithmeticUtils.timeMsToSecond(var.getIat90Duration()));
        statisticDataOut.setIat95Duration(ArithmeticUtils.timeMsToSecond(var.getIat95Duration()));
        statisticDataOut.setIat100Duration(ArithmeticUtils.timeMsToSecond(var.getIat100Duration()));
        statisticDataOut.setIat105Duration(ArithmeticUtils.timeMsToSecond(var.getIat105Duration()));
        statisticDataOut.setIat110Duration(ArithmeticUtils.timeMsToSecond(var.getIat110Duration()));
        statisticDataOut.setIat115Duration(ArithmeticUtils.timeMsToSecond(var.getIat115Duration()));
        statisticDataOut.setIat120Duration(ArithmeticUtils.timeMsToSecond(var.getIat120Duration()));
        statisticDataOut.setIat125Duration(ArithmeticUtils.timeMsToSecond(var.getIat125Duration()));
        statisticDataOut.setIat130Duration(ArithmeticUtils.timeMsToSecond(var.getIat130Duration()));
        statisticDataOut.setIat135Duration(ArithmeticUtils.timeMsToSecond(var.getIat135Duration()));
        statisticDataOut.setIat140Duration(ArithmeticUtils.timeMsToSecond(var.getIat140Duration()));
        statisticDataOut.setIat145Duration(ArithmeticUtils.timeMsToSecond(var.getIat145Duration()));
        statisticDataOut.setIat150Duration(ArithmeticUtils.timeMsToSecond(var.getIat150Duration()));
        statisticDataOut.setIatOver150Duration(ArithmeticUtils.timeMsToSecond(var.getIatOver150Duration()));

        statisticDataOut.setIatSum(var.getIatSum());
        statisticDataOut.setIatCount(var.getIatCount());
        statisticDataOut.setMaxIat(var.getMaxIat());
        statisticDataOut.setMinIat(var.getMinIat());
    }
}
